package com.future.mayi.utils;

import android.text.TextUtils;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.LogUtils;
import com.future.mayi.qsale.inter.OnCompressTaskListener;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Administrator on 2018/6/25.
 */

public class CompressImgUtils implements OnCompressTaskListener {

    private OnCompressImgListener mOnCompressImgListener;

    private List<String> resultPaths;
    private int count;

    public void compressImg(final List<String> pathList, OnCompressImgListener onCompressImgListener) {
        this.mOnCompressImgListener = onCompressImgListener;
        this.count = pathList.size();
        for (int i = 0; i < pathList.size(); i++) {
            new CompressImgTask(pathList.get(i), this).execute();
        }
    }

    @Override
    public synchronized void onSucceed(String path) {
        if (count == 0) return;
        if (resultPaths == null) {
            resultPaths = new ArrayList<>();
        }

        resultPaths.add(path);
        if (resultPaths.size() == count && mOnCompressImgListener != null) {
            mOnCompressImgListener.onSucceed(resultPaths);
        }
    }

    @Override
    public synchronized void onFailed(Throwable t) {
        if (resultPaths == null) {
            resultPaths = new ArrayList<>();
        }

        if (resultPaths.size() == count - 1) {
            if (mOnCompressImgListener != null) {
                mOnCompressImgListener.onSucceed(resultPaths);
            }
        } else {
            count = count - 1;
        }
    }

    @Override
    public void onCancel() {

    }

    public static class CompressImgTask extends ThreadUtils.SimpleTask<String> {

        private static final String TAG = "CompressImgTask";

        private String mPath;
        private OnCompressTaskListener mOnTaskListener;

        public CompressImgTask(String path, OnCompressTaskListener onTaskListener) {
            mPath = path;
            mOnTaskListener = onTaskListener;
        }

        @Override
        public String doInBackground() throws Throwable {
            if (TextUtils.isEmpty(mPath)) return "";
            return CommonUtils.getCompressImgPath(mPath);
        }

        @Override
        public void onSuccess(String result) {
            LogUtils.d(TAG, "CompressImgTask.onSuccess()" + result);
            if (mOnTaskListener != null) {
                mOnTaskListener.onSucceed(result);
            }
        }

        @Override
        public void onFail(Throwable t) {
            if (mOnTaskListener != null) {
                mOnTaskListener.onFailed(t);
            }
        }

        @Override
        public void onCancel() {
            if (mOnTaskListener != null) {
                mOnTaskListener.onCancel();
            }
        }

        public void execute() {
            ThreadUtils.executeByIo(this);
        }
    }

    public interface OnCompressImgListener {

        void onSucceed(List<String> pathList);

        void onFailed(Exception e);
    }


}
